package com.irdstudio.efp.e4a.service.impl;

import java.util.List;
import java.util.Objects;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.irdstudio.efp.e4a.service.dao.SRuleCollDtlParamDao;
import com.irdstudio.efp.e4a.service.domain.SRuleCollDtlParam;
import com.irdstudio.efp.e4a.service.facade.SRuleCollDtlParamService;
import com.irdstudio.efp.e4a.service.vo.SRuleCollDtlParamVO;
import com.irdstudio.efp.e4a.service.vo.SRuleDtlParamsVO;
import com.irdstudio.basic.framework.core.base.FrameworkService;
import com.irdstudio.basic.framework.core.util.StringUtil;

/**
 * <p>
 * ServiceImpl: 系统规则集明细参数表
 * <p>
 * 
 * @author common
 * @date 2018-05-03
 */
@Service("sRuleCollDtlParamService")
public class SRuleCollDtlParamServiceImpl implements SRuleCollDtlParamService, FrameworkService {

	private static Logger logger = LoggerFactory.getLogger(SRuleCollDtlParamServiceImpl.class);

	@Autowired
	private SRuleCollDtlParamDao sRuleCollDtlParamDao;

	/**
	 * 新增操作
	 */
	@Override
	public int insertSRuleCollDtlParam(SRuleCollDtlParamVO inSRuleCollDtlParamVo) {
		logger.debug("当前新增数据为:" + inSRuleCollDtlParamVo.toString());
		int num = 0;
		try {
			SRuleCollDtlParam sRuleCollDtlParam = new SRuleCollDtlParam();
			beanCopy(inSRuleCollDtlParamVo, sRuleCollDtlParam);
			num = sRuleCollDtlParamDao.insertSRuleCollDtlParam(sRuleCollDtlParam);
		} catch (Exception e) {
			logger.error("新增数据发生异常!", e);
			num = -1;
		}
		logger.debug("当前新增数据条数为:" + num);
		return num;
	}

	/**
	 * 删除操作
	 */
	@Override
	public int deleteByPk(SRuleCollDtlParamVO inSRuleCollDtlParamVo) {
		logger.debug("当前删除数据条件为:" + inSRuleCollDtlParamVo);
		int num = 0;
		try {
			SRuleCollDtlParam sRuleCollDtlParam = new SRuleCollDtlParam();
			beanCopy(inSRuleCollDtlParamVo, sRuleCollDtlParam);
			num = sRuleCollDtlParamDao.deleteByPk(sRuleCollDtlParam);
		} catch (Exception e) {
			logger.error("删除数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:" + inSRuleCollDtlParamVo + "删除的数据条数为" + num);
		return num;
	}

	/**
	 * 更新操作
	 */
	@Override
	public int updateByPk(SRuleCollDtlParamVO inSRuleCollDtlParamVo) {
		logger.debug("当前修改数据为:" + inSRuleCollDtlParamVo.toString());
		int num = 0;
		try {
			SRuleCollDtlParam sRuleCollDtlParam = new SRuleCollDtlParam();
			beanCopy(inSRuleCollDtlParamVo, sRuleCollDtlParam);
			num = sRuleCollDtlParamDao.updateByPk(sRuleCollDtlParam);
		} catch (Exception e) {
			logger.error("修改数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:" + inSRuleCollDtlParamVo + "修改的数据条数为" + num);
		return num;
	}

	/**
	 * 查询操作
	 */
	@Override
	public SRuleCollDtlParamVO queryByPk(SRuleCollDtlParamVO inSRuleCollDtlParamVo) {

		logger.debug("当前查询参数信息为:" + inSRuleCollDtlParamVo);
		try {
			SRuleCollDtlParam querySRuleCollDtlParam = new SRuleCollDtlParam();
			beanCopy(inSRuleCollDtlParamVo, querySRuleCollDtlParam);
			SRuleCollDtlParam queryRslSRuleCollDtlParam = sRuleCollDtlParamDao.queryByPk(querySRuleCollDtlParam);
			if (Objects.nonNull(queryRslSRuleCollDtlParam)) {
				SRuleCollDtlParamVO outSRuleCollDtlParamVo = beanCopy(queryRslSRuleCollDtlParam, new SRuleCollDtlParamVO());
				logger.debug("当前查询结果为:" + outSRuleCollDtlParamVo.toString());
				return outSRuleCollDtlParamVo;
			} else {
				logger.debug("当前查询结果为空!");
			}
		} catch (Exception e) {
			logger.error("查询数据发生异常!", e);
		}
		return null;
	}

	/**
	 * 查询用户权限数据
	 */
	@Override
	@SuppressWarnings("unchecked")
	public List<SRuleCollDtlParamVO> queryAllOwner(SRuleCollDtlParamVO sRuleCollDtlParamVo) {

		logger.debug("当前查询本人所属数据信息的参数信息为:");
		SRuleCollDtlParam sRuleCollDtlParam = beanCopy(sRuleCollDtlParamVo, new SRuleCollDtlParam());
		List<SRuleCollDtlParamVO> list = null;
		try {
			List<SRuleCollDtlParam> sRuleCollDtlParams = sRuleCollDtlParamDao.queryAllOwnerByPage(sRuleCollDtlParam);
			logger.debug("当前查询本人所属数据信息的结果集数量为:" + sRuleCollDtlParams.size());
			pageSet(sRuleCollDtlParams, sRuleCollDtlParam);
			list = (List<SRuleCollDtlParamVO>) beansCopy(sRuleCollDtlParams, SRuleCollDtlParamVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}

		return list;

	}

	/**
	 * 查询当前机构权限数据
	 */
	@Override
	@SuppressWarnings("unchecked")
	public List<SRuleCollDtlParamVO> queryAllCurrOrg(SRuleCollDtlParamVO sRuleCollDtlParamVo) {

		logger.debug("当前查询本人所属机构数据信息的参数信息为:");
		SRuleCollDtlParam sRuleCollDtlParam = beanCopy(sRuleCollDtlParamVo, new SRuleCollDtlParam());
		List<SRuleCollDtlParam> sRuleCollDtlParams = sRuleCollDtlParamDao.queryAllCurrOrgByPage(sRuleCollDtlParam);
		logger.debug("当前查询本人所属机构数据信息的结果集数量为:" + sRuleCollDtlParams.size());
		List<SRuleCollDtlParamVO> list = null;
		try {
			pageSet(sRuleCollDtlParams, sRuleCollDtlParam);
			list = (List<SRuleCollDtlParamVO>) beansCopy(sRuleCollDtlParams, SRuleCollDtlParamVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}

		return list;

	}

	/**
	 * 查询当前机构及下属机构权限数据
	 */
	@Override
	@SuppressWarnings("unchecked")
	public List<SRuleCollDtlParamVO> queryAllCurrDownOrg(SRuleCollDtlParamVO sRuleCollDtlParamVo) {

		logger.debug("当前查询本人所属机构及以下数据信息的参数信息为:");
		SRuleCollDtlParam sRuleCollDtlParam = beanCopy(sRuleCollDtlParamVo, new SRuleCollDtlParam());
		List<SRuleCollDtlParam> sRuleCollDtlParams = sRuleCollDtlParamDao.queryAllCurrDownOrgByPage(sRuleCollDtlParam);
		logger.debug("当前查询本人所属机构及以下数据信息的结果集数量为:" + sRuleCollDtlParams.size());
		List<SRuleCollDtlParamVO> list = null;
		try {
			pageSet(sRuleCollDtlParams, sRuleCollDtlParam);
			list = (List<SRuleCollDtlParamVO>) beansCopy(sRuleCollDtlParams, SRuleCollDtlParamVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}

		return list;

	}

	@Override
	public List<SRuleCollDtlParamVO> queryByRuleCollDtlId(String collDtlId) {
		logger.debug("当前根据配置规则ID" + collDtlId + "查询对应参数信息开始......");
		try {
			List<SRuleCollDtlParam> queryRslSRuleCollDtlParam = sRuleCollDtlParamDao.queryByRuleCollDtlId(collDtlId);
			if (Objects.nonNull(queryRslSRuleCollDtlParam)) {
				List<SRuleCollDtlParamVO> outSRuleCollDtlParamVo = (List<SRuleCollDtlParamVO>) beansCopy(queryRslSRuleCollDtlParam,
						SRuleCollDtlParamVO.class);
				logger.debug("当前根据配置规则ID" + collDtlId + "查询对应参数信息结束......");
				return outSRuleCollDtlParamVo;
			} else {
				logger.debug("当前查询结果为空!");
			}
		} catch (Exception e) {
			logger.error("查询数据发生异常!", e);
		}
		return null;
	}

	/**
	 * 批量更新配置参数值
	 */
	@Override
	public int batchUpdateParamVal(SRuleDtlParamsVO vo) {

		logger.info("规则配置编号：" + vo.getDtlId() + "批量更新配置值开始......");
		int num = 0;
		if (Objects.nonNull(vo.getData()) && StringUtil.isNotEmpty(vo.getDtlId()) && !vo.getData().isEmpty()) {
			try {
				@SuppressWarnings("unchecked")
				List<SRuleCollDtlParam> params = (List<SRuleCollDtlParam>) beansCopy(vo.getData(), SRuleCollDtlParam.class);
				params.parallelStream().forEach(s -> {
					s.setRuleName(null);
					s.setDtlId(vo.getDtlId());
					setUpdateDefaultProperty(s);
				});
				for (SRuleCollDtlParam param : params) {
					num += sRuleCollDtlParamDao.updateParamValByPkId(param);
				}
				logger.info("规则配置编号：" + vo.getDtlId() + "批量更新配置值结束!");
			} catch (Exception e) {
				logger.info("规则配置编号：" + vo.getDtlId() + "批量更新配置值出现异常!", e);
				num = -1;
			}
		}

		return num;

	}
}
